package com.itheima.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.DTO.CheckGroupDTO;
import com.itheima.common.entity.PageResult;
import com.itheima.mapper.CheckGoupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImp implements CheckGroupService {

    @Autowired
    private CheckGoupMapper checkGoupMapper;
    /*
     * 新增检查组，同时需要关联检查项
     */
    @Transactional
    public void add(CheckGroup checkGroup, List<String> idList ) {
        checkGoupMapper.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        if (idList != null && idList.size() > 0) {
            for (String id : idList) {
                checkGoupMapper.setCheckGroupAndCheckItem(checkGroupId, Integer.parseInt(id));
            }
        }
    }

    /**
     * 根据检查组id删除检查组
     * @param id
     */
    @Transactional
    public void deleteById(Integer id) {
        checkGoupMapper.deleteById(id);
        checkGoupMapper.deleteCheckGroupAndCheckItem(id);
    }


    /**
     * 根据检查组id查询检查项id
     * @param checkGroupId
     * @return
     */
    public List<Integer> findCheckGroupIdsBySetmealId(Integer checkGroupId){
        return checkGoupMapper.findCheckGroupIdsBySetmealId(checkGroupId);
    }

    /**
     * 分页查询
     * @param checkGroupDTO
     * @return
     */
    public PageResult pageQuery(CheckGroupDTO checkGroupDTO) {
        Integer currentPage = checkGroupDTO.getCurrentPage();
        Integer pageSize = checkGroupDTO.getPageSize();
        String queryString = checkGroupDTO.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkGoupMapper.pageQuery(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }


   /**
     * 根据检查组id查询检查组
     * @param id
     * @return
     */
    public CheckGroup findById(Integer id){

        CheckGroup checkGroup = checkGoupMapper.findById(id);
        if (checkGroup != null) {
            List<CheckItem> checkItems = checkGoupMapper.findCheckItemByCheckGroupId(id);
            checkGroup.setCheckItems(checkItems);
        }
        return checkGroup;
    }

    /**
     * 查询所有检查组
     * @return
     */
    public  List<CheckGroup> findAll(){
        List<CheckGroup>  checkGroups = checkGoupMapper.findAll();
        if (checkGroups != null && checkGroups.size() > 0) {
            for (CheckGroup checkGroup : checkGroups) {
                List<CheckItem> checkItems = checkGoupMapper.findCheckItemByCheckGroupId(checkGroup.getId());
                checkGroup.setCheckItems(checkItems);
            }
        }

        return checkGroups;
    }

    /**
     * 编辑检查组
     * @param checkgroup
     * @param checkitemIds
     */
    @Transactional
    public void edit(CheckGroup checkgroup, List<String> idList) {
        checkGoupMapper.deleteCheckGroupAndCheckItem(checkgroup.getId());

        if (idList != null && idList.size() > 0) {
            for (String id : idList) {
                checkGoupMapper.setCheckGroupAndCheckItem(checkgroup.getId(), Integer.parseInt(id));
            }
        }

        checkGoupMapper.edit(checkgroup);
    }

}
